package com.crm.biz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.BizProductPrice;
import com.crm.model.ProductPrice;
import com.crm.model.ProductPriceModel;
import com.crm.service.BizProductPriceService;
import com.crm.util.JsonUtil;
import com.crm.util.ValidateUtil;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/bizProductPrice")
public class BizProductPriceController {
	
	@Autowired
	private BizProductPriceService bizProductPriceService;
	
	@RequestMapping("/index")
	public String index(){
		return "configuration/productPricing";
	}
	
	@RequestMapping("/index2")
	public String index2(String productId, Integer from, ModelMap map){
		
		map.put("productId", productId);
		map.put("from", from);
		
		return "configuration/productPricing2";
	}
	
	@RequestMapping("/getAll")
	@ResponseBody
	public Map<String, Object> getAll(Page<BizProductPrice> page, BizProductPrice t){
		Map<String, Object> map = new HashMap<>();
		Page<BizProductPrice> list = (Page<BizProductPrice>) bizProductPriceService.selectAll(page, t);
		
		map.put("list", list);
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/addNewRecord")
	@ResponseBody
	public Map<String, Object> addNewRecord(ProductPriceModel productPrice, String[] departments, String productId, String content){
		
		List<ProductPrice> list = productPrice.getProductPrice();
		
		Map<String, Object> map = new HashMap<>();
		
		if(ValidateUtil.isEmpty(productId))
			throw new RuntimeException("产品ID不能为空！");
		
		if(null == departments || departments.length <= 0)
			throw new RuntimeException("没有选择任何分校！");
		
		if(productId.indexOf(",") > 0){
			//批量设置
			String[] productIds = productId.split(",");
			for(String pId : productIds){
				bizProductPriceService.insertBatch(JsonUtil.toJsonStr(list), departments, pId, content);
			}
		}else{
			bizProductPriceService.insertBatch(JsonUtil.toJsonStr(list), departments, productId, content);
		}
		
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public Map<String, Object> updateRecord(BizProductPrice record){
		
		Map<String, Object> map = new HashMap<>();
		
		bizProductPriceService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "修改成功！");
		
		return map;
	}
	
	
	
}
